#ifndef SERVO_H
#define SERVO_H

/*
  ServoTimeTimer1 - Hardware serial library based on ServoTimer1 by Jim Studt, jim@federated.com
  Author: Thomas Ouellet Fredericks, tof@danslchamp.org
  Copyright (c) 2007 David A. Mellis.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <inttypes.h>

#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )

typedef struct
{
  uint8_t angle;
  uint8_t pin;
  uint16_t ms;
} ServoState;

void initServo();
//static void releaseTimer1();

// attach to a pin, sets pinMode, returns 0 on failure, won't
uint8_t attachServo(ServoState *state);
// position the servo until a subsequent write() happens
// Only works for 9 and 10.

void detachServo(ServoState *state);

// specify the angle in microseconds ( 500 to 2500)
void write_ms(ServoState *state);

// specify the angle in degrees ( 0 to 180)
void write_deg(ServoState *state);

// returns the angle in microseconds ( 500 to 2500)
int read();

uint8_t attached();

#endif
